//const _ = require('lodash');
const merchantDao = require("../../dao/merchantDao.js");
var foodDao = require('../../dao/foodDao.js');
const winston = require("../../utils/winstonLogger.js");
const _ = require('lodash');

module.exports = {
    getAllPassMerchantsFromA: getAllPassMerchantsFromA,
    getMerchantsByFoodType: getMerchantsByFoodType
}



function getAllPassMerchantsFromA(req, res, next){
    merchantDao.findMerchant({status:{$eq: "正常"}}, (err, result) => {
        if(err){
           winston.error("getAllPassMerchantsFromA......error.......");
        }else{
            res.json(result);
        }
    })
}

  // 根据食品类型加载商家信息：1、先找出该类型的所有食品，
  // 就可以拿到merchant(merchantId)，然后根据 merchantId， 去找到对应的商家 


  function getMerchantsByFoodType(req, res, next){
    let foodType = req.params.foodType;
    if(foodType == 1) {foodType = "酒水"}
    else if(foodType == 2) {foodType = "套餐"}
    else if(foodType == 3) {foodType = "主食"}
    else if(foodType == 4) {foodType = "小吃"}
    console.log('_______________'+foodType);
    foodDao.queryFoodByType({type:foodType}, (err, result)=>{
        let merchants = [];        
        if(err){
            winston.error("query error......");
        }else{
            _.forEach(result, (item) => {
                winston.info("item__"+item.merchant);
                merchants.push(item.merchant);
            })
           res.json(merchants);
        }
    });
}









// function getMerchantsByFoodType(req, res, next){
//     let foodType = req.params.foodType;
//     if(foodType == 1) {foodType = "酒水"}
//     else if(foodType == 2) {foodType = "套餐"}
//     else if(foodType == 3) {foodType = "主食"}
//     else if(foodType == 4) {foodType = "小吃"}
//     console.log('_______________'+foodType);
//     foodDao.queryFood({type:foodType}, (err, result)=>{
//         var merchants = [];        
//         if(err){
//             winston.error("query error......");
//         }else{
//             winston.info("getMerchantsByFoodType 查询出来的信息为： "+result);
//             _.forEach(result, (item) => {
//                 winston.info("-------------------item.merchant------------------"+item.merchant);
//                 merchantDao.findMerchant({_id: item.merchant}, (e, ms) => {
//                     winston.info("查询出来的商家信息为；"+ms[0].username+" "+ms[0].name);
//                     merchants.push(ms[0]);
//                     winston.info("---------------merchants----------"+merchants+"++++++++++");
                    
//                 });
//             });
//             winston.info("---------------merchants----------"+merchants.length+"++++++++++");
//             res.json(merchants);
//         }
//     });
// }

